﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;
namespace FlexControls
{
    public class clsFlex
    {
#region Form System
        //Buttons state
        public const int CLICK_LUU = 0;
        public const int CLICK_CAPNHAT = 1;
        public const int CLICK_XOA = 2;
        public const int CLICK_THOAT = 3;
        public const int CLICK_IN = 4;
        public const int CLICK_THEMMOI = 5;
        //
        public const int FORM_MAX=30;
        public struct ControlSys
        {
            public int TEXTBOX_MAX;
            public int DGVIEW_MAX;
            public int intSoTextBox;
            public int intSoDGView;
            public int GenCodeID;
            public FlexTextBox[] txt;
            public FlexDataGridView[] dgv;
            public FlexButtons Buttons;
        }
        public struct FormSys
        {
            //Form
            public int intSoForm;
            public System.Windows.Forms.Form[] frm;
            //Textbox
            //Tables
            public string[] TableName;
            public ControlSys[] CtrlSys;
            //Xml
            public XMLData[] xmlD;
            //only for form gridview
            public int ParentFormID;
            public string ctrlTag;
        }
        public FormSys clsForm = new FormSys();
        public clsFlex()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            try
            {
                clsForm.intSoForm = 0;
                clsForm.frm = new System.Windows.Forms.Form[FORM_MAX];
                clsForm.TableName = new string[FORM_MAX];
                clsForm.CtrlSys = new ControlSys[FORM_MAX];
                clsForm.xmlD = new XMLData[FORM_MAX];
            }
            catch (Exception ex)
            {
                MessageBox.Show("InitializeComponent ... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            
            
        }
        public int KhoiTaoForm(System.Windows.Forms.Form frm,string strTableName)
        {
            try
            {
                clsForm.frm[clsForm.intSoForm] = frm;
                clsForm.TableName[clsForm.intSoForm] = strTableName;
                clsForm.CtrlSys[clsForm.intSoForm].intSoTextBox = 0;
                clsForm.CtrlSys[clsForm.intSoForm].intSoDGView = 0;
                clsForm.CtrlSys[clsForm.intSoForm].dgv = new FlexDataGridView[1];
                clsForm.CtrlSys[clsForm.intSoForm].Buttons = new FlexButtons();
                clsForm.CtrlSys[clsForm.intSoForm].GenCodeID =-1;
                clsForm.ParentFormID = -1;
                clsForm.ctrlTag = "";
                clsForm.intSoForm++;
                return clsForm.intSoForm - 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("KhoiTaoForm... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
                return -1;
            }
            
        }
        public void CallForm(int FormId,string strFormTitle)
        {
            try
            {
                clsForm.frm[FormId].Text = strFormTitle;
                clsForm.CtrlSys[FormId].Buttons.ChangeState(CLICK_LUU);
                ClearControls(FormId);
                clsForm.frm[FormId].Show();
                AddDataToDataGridView(FormId);
                GenerateCode(FormId);
            }
            catch (Exception ex)
            {
                MessageBox.Show("CallForm... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            
        }
        public void CallFormGridView(int FormId,int thisFormID,int linkFormID,string ctrlTag)
        {
            try
            {
                clsForm.TableName[FormId] = clsForm.TableName[linkFormID];
                clsForm.xmlD[FormId] = clsForm.xmlD[linkFormID];
                clsForm.xmlD[FormId].SoColumn = clsForm.xmlD[linkFormID].SoColumn;
                clsForm.frm[FormId].Show();
                clsForm.ParentFormID = thisFormID;
                clsForm.ctrlTag = ctrlTag;
                
                AddDataToDataGridView(FormId);
            }
            catch (Exception ex)
            {
                MessageBox.Show("CallForm... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }

        }
        public void FormGridViewClosing(string strCode,string strText)
        {
            for (int i = 0; i < clsForm.CtrlSys[clsForm.ParentFormID].intSoTextBox; i++)
            {
                if (clsForm.CtrlSys[clsForm.ParentFormID].txt[i].booLinkTalbe == true && clsForm.CtrlSys[clsForm.ParentFormID].txt[i].Tag==clsForm.ctrlTag)
                {
                    clsForm.CtrlSys[clsForm.ParentFormID].txt[i].fcText = strText;
                    clsForm.CtrlSys[clsForm.ParentFormID].txt[i].strCode = strCode;
                    clsForm.ParentFormID = -1;
                    break;
                }
            }
        }
        public void KhoiTaoFormControls(int FormID)
        {
            try
            {
                int intCount = 0;
                for (int i = 0; i < clsForm.frm[FormID].Controls.Count; i++)
                {
                    if (clsForm.frm[FormID].Controls[i].Tag != null)
                    {
                        for (int j = 0; j < clsForm.xmlD[FormID].SoColumn; j++)
                        {
                            if (clsForm.frm[FormID].Controls[i].Tag.ToString() == clsForm.xmlD[FormID].Names[j].ToString())
                            {
                                intCount++;
                            }
                        }
                    }
                }
                clsForm.CtrlSys[FormID].TEXTBOX_MAX = intCount;
                clsForm.CtrlSys[FormID].txt = new FlexTextBox[intCount];
                for (int i = 0; i < clsForm.frm[FormID].Controls.Count; i++)
                {
                    if (clsForm.frm[FormID].Controls[i].Tag != null)
                    {
                        for (int j = 0; j < clsForm.xmlD[FormID].SoColumn; j++)
                        {
                            if (clsForm.frm[FormID].Controls[i].Tag.ToString() == clsForm.xmlD[FormID].Names[j].ToString())
                            { 
                                
                                clsForm.CtrlSys[FormID].txt[clsForm.CtrlSys[FormID].intSoTextBox] =(FlexTextBox) clsForm.frm[FormID].Controls[i];
                                if (clsForm.CtrlSys[FormID].txt[clsForm.CtrlSys[FormID].intSoTextBox].strGenCode != "")
                                {
                                    clsForm.CtrlSys[FormID].GenCodeID = clsForm.CtrlSys[FormID].intSoTextBox;
                                    clsForm.CtrlSys[FormID].txt[clsForm.CtrlSys[FormID].intSoTextBox].Enabled = false;
                                }
                                clsForm.CtrlSys[FormID].intSoTextBox++;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("InitForm Textbox... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        }
        public void KhoiTaoFormControls(int FormID,FlexDataGridView dgv)
        {
            try
            {
                clsForm.CtrlSys[FormID].intSoDGView++;
                clsForm.CtrlSys[FormID].dgv[0] = dgv;
            }
            catch (Exception ex)
            {
                MessageBox.Show("InitForm drigView... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        }
        public void KhoiTaoFormControls(int FormID,FlexButtons but)
        {
            try
            {
                clsForm.CtrlSys[FormID].Buttons = but;
            }
            catch (Exception ex)
            {
                MessageBox.Show("KhoiTaoFormControls Button... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        }
#endregion
#region "XML"
        public static string strTableName="";
        public struct XMLData
        {
            public int SoColumn;
            public string TableName;
            public string[] ColumnName;
            public string[] Names;
            public string[] Value;
        };
        private const string DATA_PATH = "..\\..\\DATA\\";
        public  void CreateXMLTable(string strTableName, XMLData xmlD)
        {
            try
            {
                XmlTextWriter xWrite;

                xWrite = new XmlTextWriter(DATA_PATH + strTableName + ".xml", System.Text.Encoding.UTF8);

                xWrite.WriteStartDocument(true);
                xWrite.Formatting = Formatting.Indented;
                xWrite.Indentation = 3;
                xWrite.WriteStartElement(strTableName + "s");
                xWrite.WriteStartElement("ColumnNames");
                for (int i = 0; i < xmlD.SoColumn; i++)
                {
                    xWrite.WriteStartElement(xmlD.Names[i]);
                    xWrite.WriteString(xmlD.ColumnName[i]);
                    xWrite.WriteEndElement();
                }
                xWrite.WriteEndElement();
                xWrite.WriteEndElement();

                xWrite.WriteEndDocument();

                xWrite.Flush();
                xWrite.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("CreateXMLTable... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            


        }
        public  bool FindAElement(string strTableName, string strColumnName, string strElement)
        {
            try
            {
                XElement xelement = XElement.Load(DATA_PATH + strTableName + ".xml");
                var addresses = from address in xelement.Elements(strTableName)
                                where (string)address.Element(strColumnName) == strElement
                                select address;
                foreach (XElement xEle in addresses)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("FindAElement... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
                return false;
            }
            

        }
        
        public  void KhoiTaoXMLTable(int FormID, int intSoColumn)
        {
            try
            {
                clsForm.xmlD[FormID].TableName = clsForm.TableName[FormID];
                clsForm.xmlD[FormID].SoColumn = intSoColumn;

                clsForm.xmlD[FormID].ColumnName = new string[intSoColumn];
                clsForm.xmlD[FormID].Names = new string[intSoColumn+1];
                clsForm.xmlD[FormID].Value = new string[intSoColumn+1];
                for (int i = 0; i < intSoColumn; i++)
                {
                    clsForm.xmlD[FormID].Value[i] = "Empty";

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("KhoiTaoXMLTable... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            
        }
        public  void LoadData(System.Windows.Forms.Form frm, XMLData a)
        {
            try
            {
                for (int i = 0; i < frm.Controls.Count; i++)
                {
                    if (frm.Controls[i].Tag != null)
                        for (int j = 0; j < a.SoColumn; j++)
                        {

                            if (a.Names[j] == frm.Controls[i].Tag.ToString())
                                frm.Controls[i].Text = a.Value[j];
                        }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("LoadData... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            
        }
        public  void SaveData(System.Windows.Forms.Form frm, XMLData a)
        {
            try
            {
                for (int i = 0; i < frm.Controls.Count; i++)
                {
                    if (frm.Controls[i].Tag != null)
                        for (int j = 0; j < a.SoColumn; j++)
                        {

                            if (a.Names[j] == frm.Controls[i].Tag.ToString())
                            {
                                a.Value[j] = frm.Controls[i].Text;
                                frm.Controls[i].Text = "";
                            }
                        }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("SaveData... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            
        }
        public  void ReadXML(string strTableName, XMLData a)
        {
            try
            {
                XElement xelement = XElement.Load(DATA_PATH + strTableName + ".xml");
                IEnumerable<XElement> Columns = xelement.Elements();
                foreach (var col in Columns)
                {
                    if (col.Name != "ColumnNames")
                    {
                        for (int j = 0; j < a.SoColumn; j++)
                        {
                            a.Value[j] = col.Element(a.Names[j]).Value;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("ReadXML... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
           
        }
#endregion 
#region Data
        public void InsertElement(int FormID)
        {
            try
            {
                if (CheckValidate(FormID) == true)
                {
                    
                    for (int i = 0; i < clsForm.CtrlSys[FormID].intSoTextBox; i++)
                    {
                        string strText = "";
                        if (clsForm.CtrlSys[FormID].txt[i].booLinkTalbe == false)
                            strText = clsForm.CtrlSys[FormID].txt[i].fcText;
                        else
                            strText = "!0!";
                        for (int j = 0; j < clsForm.xmlD[FormID].SoColumn; j++)
                        {

                            if (clsForm.xmlD[FormID].Names[j] == clsForm.CtrlSys[FormID].txt[i].Tag.ToString())
                            {
                                clsForm.xmlD[FormID].Value[j] = strText;
                                break;
                            }
                        }
                    }
                    if (FindAElement(clsForm.TableName[FormID], clsForm.xmlD[FormID].Names[0], clsForm.xmlD[FormID].Value[0]) != true)
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(DATA_PATH + clsForm.TableName[FormID] + ".xml");
                        XmlElement xEle = xmlDoc.CreateElement(clsForm.TableName[FormID]);
                        for (int i = 0; i < clsForm.xmlD[FormID].SoColumn; i++)
                        {

                            XmlElement xEleChild = xmlDoc.CreateElement(clsForm.xmlD[FormID].Names[i]);
                            if (clsForm.xmlD[FormID].Value[i].ToString() != "!0!")
                                xEleChild.InnerText = clsForm.xmlD[FormID].Value[i].ToString();
                            else
                            {
                                XmlElement xEleChildCName = xmlDoc.CreateElement("NAME");
                                for (int k = 0; k < clsForm.CtrlSys[FormID].intSoTextBox; k++)
                                {

                                    if (clsForm.CtrlSys[FormID].txt[k].booLinkTalbe == true && clsForm.CtrlSys[FormID].txt[k].Tag == clsForm.xmlD[FormID].Names[i])
                                    {
                                        xEleChildCName.InnerText = clsForm.CtrlSys[FormID].txt[k].strCode;
                                        break;
                                    }
                                }
                                XmlElement xEleChildCValue = xmlDoc.CreateElement("VALUE");
                                for (int k = 0; k < clsForm.CtrlSys[FormID].intSoTextBox; k++)
                                {

                                    if (clsForm.CtrlSys[FormID].txt[k].booLinkTalbe == true && clsForm.CtrlSys[FormID].txt[k].Tag == clsForm.xmlD[FormID].Names[i])
                                    {
                                        xEleChildCValue.InnerText = clsForm.CtrlSys[FormID].txt[k].fcText;
                                        break;
                                    }
                                }
                                xEleChild.AppendChild(xEleChildCName);
                                xEleChild.AppendChild(xEleChildCValue);
                            }
                            xEle.AppendChild(xEleChild);
                        }
                        xmlDoc.DocumentElement.AppendChild(xEle);
                        xmlDoc.Save(DATA_PATH + clsForm.TableName[FormID] + ".xml");
                        ClearControls(FormID);
                    }
                    else
                    {
                        MessageBox.Show("Dữ liệu bạn nhập đã có sẵn");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("InsertElement... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }

        }
        public  void AddDataToDataGridView(int FormID )
        {
            try
            {
                if (clsForm.CtrlSys[FormID].intSoDGView > 0)
                {
                    XElement xelement = XElement.Load(DATA_PATH + clsForm.TableName[FormID] + ".xml");
                    IEnumerable<XElement> Columns = xelement.Elements();
                    // Read the entire XML
                    //int j = 0;
                    System.Data.DataTable dt = new System.Data.DataTable();
                    foreach (XElement col in Columns)
                    {
                        if (col.Name == "ColumnNames")
                        {
                            for (int j = 0; j < clsForm.xmlD[FormID].SoColumn; j++)
                            {
                                dt.Columns.Add(col.Element(clsForm.xmlD[FormID].Names[j]).Value, typeof(string));

                            }
                        }
                        else
                        {

                            System.Data.DataRow row = dt.NewRow();
                            for (int j = 0; j < clsForm.xmlD[FormID].SoColumn; j++)
                            {
                                row[j] = col.Element(clsForm.xmlD[FormID].Names[j]).Value;
                                foreach (XElement colChild in col.Elements(clsForm.xmlD[FormID].Names[j]))
                                {
                                    if (colChild.Element("VALUE") != null)
                                        row[j] = colChild.Element("VALUE").Value.ToString();
                                }


                            }
                            dt.Rows.Add(row);
                        }
                    }

                    clsForm.CtrlSys[FormID].dgv[0].dgv.DataSource = dt;
                    for (int i = 0; i < clsForm.xmlD[FormID].SoColumn; i++)
                    {
                        clsForm.CtrlSys[FormID].dgv[0].dgv.Columns[i].Tag = clsForm.xmlD[FormID].Names[i];
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("AddDataToDataGridView... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        
            
        }
        
        public  void AddDataToControls(int FormID)
        {
            try
            {
                for (int i = 0; i < clsForm.CtrlSys[FormID].intSoTextBox; i++)
                {
                    for (int j = 0; j < clsForm.xmlD[FormID].SoColumn; j++)
                    {

                        if (clsForm.CtrlSys[FormID].dgv[0].dgv.Columns[clsForm.CtrlSys[FormID].dgv[0].dgv.SelectedCells[j].ColumnIndex].Tag.ToString() == clsForm.CtrlSys[FormID].txt[i].Tag.ToString())
                            clsForm.CtrlSys[FormID].txt[i].fcText = clsForm.CtrlSys[FormID].dgv[0].dgv.SelectedCells[j].Value.ToString();
                    }
                }
                clsForm.CtrlSys[FormID].Buttons.ChangeState(clsFlex.CLICK_CAPNHAT);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Add data to controls... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            
        }
        public  void DeleteData(int FormID)
        {
            try
            {
                if (System.Windows.Forms.MessageBox.Show("Bạn có chắc muốn xóa?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    string strElement = clsForm.CtrlSys[FormID].dgv[0].dgv.SelectedCells[0].Value.ToString();
                    XElement xEmp = XElement.Load(DATA_PATH + clsForm.TableName[FormID] + ".xml");
                    //
                    var empDetails = from emps in xEmp.Elements(clsForm.TableName[FormID])
                                     where emps.Element(clsForm.xmlD[FormID].Names[0]).Value.Equals(strElement)
                                     select emps;
                    //
                    empDetails.First().Remove();
                    //
                    xEmp.Save(DATA_PATH + clsForm.TableName[FormID] + ".xml");
                    ClearControls(FormID);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("DeleteData... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            

        }
        public  void ClearControls(int FormID)
        {
            try
            {
                for (int i = 0; i < clsForm.CtrlSys[FormID].intSoTextBox; i++)
                {
                    clsForm.CtrlSys[FormID].txt[i].fcText = "";
                }
                GenerateCode(FormID);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ClearControls... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            
        }
        public  void UpdateData(int FormID)
        {
            try
            {
                if (CheckValidate(FormID) == true)
                {
                    string strElement = clsForm.CtrlSys[FormID].dgv[0].dgv.SelectedCells[0].Value.ToString();
                    for (int i = 0; i < clsForm.CtrlSys[FormID].intSoTextBox; i++)
                    {
                        string strText = "";
                        if (clsForm.CtrlSys[FormID].txt[i].booLinkTalbe == false)
                            strText = clsForm.CtrlSys[FormID].txt[i].fcText;
                        else
                            strText = "!0!";
                        for (int j = 0; j < clsForm.xmlD[FormID].SoColumn; j++)
                        {

                            if (clsForm.xmlD[FormID].Names[j] == clsForm.CtrlSys[FormID].txt[i].Tag.ToString())
                            {
                                clsForm.xmlD[FormID].Value[j] = strText;
                                break;
                            }
                        }
                    }
                    XElement xEmp = XElement.Load(DATA_PATH + clsForm.TableName[FormID] + ".xml");
                    //
                    var empDetails = from emps in xEmp.Elements(clsForm.TableName[FormID])
                                     where emps.Element(clsForm.xmlD[FormID].Names[0]).Value.Equals(strElement)
                                     select emps;
                    foreach (XElement xEle in empDetails)
                    {
                        for (int i = 0; i < clsForm.xmlD[FormID].SoColumn; i++)
                        {
                            if (clsForm.xmlD[FormID].Value[i].ToString() != "!0!")
                                xEle.Element(clsForm.xmlD[FormID].Names[i]).Value = clsForm.xmlD[FormID].Value[i];
                            else 
                            {
                                XmlDocument xmlDoc = new XmlDocument();
                                XmlElement xEleChildCName = xmlDoc.CreateElement("NAME");
                                for (int k = 0; k < clsForm.CtrlSys[FormID].intSoTextBox; k++)
                                {

                                    if (clsForm.CtrlSys[FormID].txt[k].booLinkTalbe == true && clsForm.CtrlSys[FormID].txt[k].Tag == clsForm.xmlD[FormID].Names[i])
                                    {
                                        xEle.Element(clsForm.xmlD[FormID].Names[i]).Remove();
                                        xEle.Add(new XElement (clsForm.xmlD[FormID].Names[i],new XElement("NAME",clsForm.CtrlSys[FormID].txt[k].strCode),new XElement("VALUE",clsForm.CtrlSys[FormID].txt[k].fcText)));
                                        break;
                                    }
                                }
                                XmlElement xEleChildCValue = xmlDoc.CreateElement("VALUE");
                                for (int k = 0; k < clsForm.CtrlSys[FormID].intSoTextBox; k++)
                                {

                                    if (clsForm.CtrlSys[FormID].txt[k].booLinkTalbe == true && clsForm.CtrlSys[FormID].txt[k].Tag == clsForm.xmlD[FormID].Names[i])
                                    {
                                        xEleChildCValue.InnerText = clsForm.CtrlSys[FormID].txt[k].fcText;
                                        break;
                                    }
                                }
                                
                            }
                        }

                    }
                    xEmp.Save(DATA_PATH + clsForm.TableName[FormID] + ".xml");
                    ClearControls(FormID);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
            
        }
        public void ButtonsClick(int FormID,int intState)
        {
            try
            {

                switch (intState)
                {
                    case CLICK_THEMMOI:
                        ClearControls(FormID);
                        clsForm.CtrlSys[FormID].Buttons.ChangeState(clsFlex.CLICK_LUU);
                        break;
                    case CLICK_LUU:
                        InsertElement(FormID);
                        AddDataToDataGridView(FormID);        
                        break;
                    case CLICK_CAPNHAT:
                        UpdateData(FormID);
                        AddDataToDataGridView(FormID);
                        clsForm.CtrlSys[FormID].Buttons.ChangeState(clsFlex.CLICK_LUU);
                        break;
                    case CLICK_XOA:
                        DeleteData(FormID);
                        AddDataToDataGridView(FormID);
                        clsForm.CtrlSys[FormID].Buttons.ChangeState(clsFlex.CLICK_LUU);
                        break;
                    case CLICK_IN:
                        break;
                    case CLICK_THOAT:
                        clsForm.frm[FormID].Visible = false;
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Buttons click void... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        }
        public bool CheckValidate(int FormID)
        {
            try
            {
                for (int i = 0; i < clsForm.CtrlSys[FormID].intSoTextBox; i++)
                {
                    if (clsForm.CtrlSys[FormID].txt[i].booValidate == true)
                    {
                        if (clsForm.CtrlSys[FormID].txt[i].fcText == "")
                        {
                            MessageBox.Show("Hãy nhập đầy đủ thông tin");
                            return false;
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Check validate... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
                return false;
            }
        }
        public void GenerateCode(int FormID)
        {
            if (clsForm.CtrlSys[FormID].GenCodeID > -1)
            {
                string strCode=clsForm.CtrlSys[FormID].txt[clsForm.CtrlSys[FormID].GenCodeID].fcGenerateCode;
                int Number=1;
                XElement xelement = XElement.Load(DATA_PATH + clsForm.TableName[FormID] + ".xml");
                IEnumerable<XElement> Columns = xelement.Elements();
                bool flag = false;
                while (flag==false)
                {
                    foreach (var col in Columns)
                    {
                        flag = true;
                        if (col.Name != "ColumnNames")
                        {
                            string strTag;
                            strTag = col.Element(clsForm.xmlD[FormID].Names[0]).Value;
                            
                            if (Number == GetIdFormInt(strTag))
                            {
                                Number++;
                                flag = false;
                                break;
                            }
                            
                        }
                    }
                }
                if (Number > 9)
                {
                    strCode += Convert.ToString(Number);
                }
                else
                {
                    strCode += "0"+ Convert.ToString(Number);
                }
                clsForm.CtrlSys[FormID].txt[clsForm.CtrlSys[FormID].GenCodeID].fcText = strCode;
            }
            
        }
        private int GetIdFormInt(string strTag)
        {
            string strValue = Convert.ToString(strTag);
            char[] delimiters = new char[] { ' ','C','G','V', 'D', 'T', 'S', 'H', 'K', 'S', 'M', 'F', 'P', 'L', 'N', 'U', '_' };
            string[] strCode = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            return Convert.ToInt32(strCode[0]);

        }
#endregion
    }
}
